using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Admin_Controls_UserRolesList : IWS.Lib.Utilities.Web.ListControl
{
	protected void Page_Load(object sender, EventArgs e)
	{
		this.lblNotFound.Visible = false;

		if (!Page.IsPostBack)
		{
			this.Initialize();
		}
	}


	protected override void InitializeCustom()
	{
		//load roles into drop down. 
		string[] roles = System.Web.Security.Roles.Provider.GetAllRoles();
		this.ddlRoles.DataSource = roles;
		this.ddlRoles.DataBind();
	}

	public void LoadRolesByUser(string username)
	{
		this.Initialize(); //always call initialize in case this function is fired before the page_load event. 

		this.lblUsername.Text = username;
		string[] roles = System.Web.Security.Roles.Provider.GetRolesForUser(username);
		this.grdUserRoles.DataSource = roles;
		this.grdUserRoles.DataBind();

		this.lblNotFound.Visible = (roles == null || roles.Length == 0);
		this.grdUserRoles.Visible = (roles != null && roles.Length > 0);
	}
	protected void cmdAddToGroup_Click(object sender, EventArgs e)
	{
		ListItem item = this.ddlRoles.SelectedItem;
		if (item != null)
		{
			System.Web.Security.Roles.Provider.AddUsersToRoles(new string[] {lblUsername.Text}, new string[] {item.Value});
		}

		//refresh control.
		this.LoadRolesByUser(lblUsername.Text);
	}
	protected void grdUserRoles_RowDeleting(object sender, GridViewDeleteEventArgs e)
	{
		int i = 0;
		Label lblID = this.grdUserRoles.Rows[e.RowIndex].FindControl("lblID") as Label;
		if (lblID != null)
		{
			System.Web.Security.Roles.Provider.RemoveUsersFromRoles(new string[] { lblUsername.Text }, new string[] {lblID.Text });
		}
		this.LoadRolesByUser(lblUsername.Text);
	}
}
